package com.tjvib.sensor;

import java.io.OutputStreamWriter;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * 传感器抽象类
 *
 * @param <V> 传感器数据类型
 */
public abstract class Sensor<V extends SensorData> {

    // Streaming Frequency
    public static final int STREAM_FREQ_5HZ = 5;
    public static final int STREAM_FREQ_10HZ = 10;
    public static final int STREAM_FREQ_25HZ = 25;
    public static final int STREAM_FREQ_50HZ = 50;
    public static final int STREAM_FREQ_100HZ = 100;
    public static final int STREAM_FREQ_200HZ = 200;
    public static final int STREAM_FREQ_400HZ = 400;
    // Connection status
    public static final int SENSOR_STATUS_CONNECTED = 1;
    public static final int SENSOR_STATUS_CONNECTING = 2;
    public static final int SENSOR_STATUS_DISCONNECTED = 3;

    static OutputStreamWriter tmpFileWriter;

    LinkedBlockingDeque<V> dataQueue = new LinkedBlockingDeque<>(1000);

    int streamingFrequency = STREAM_FREQ_100HZ;
    int connectionStatus = SENSOR_STATUS_DISCONNECTED;

    abstract public boolean connect();

    abstract public void disconnect();

    abstract public void setStreamFrequency(final int freq);

    public int getConnectionStatus() {
        return connectionStatus;
    }

    abstract public void resetTimestamp();

    public int getStreamFrequency() {
        return streamingFrequency;
    }

    public int hasNewData() {
        int n;
        synchronized (dataQueue) {
            n = dataQueue.size();
        }
        return n;
    }

    public abstract V getData();

}
